subroutine ccsdt_sym_bba_dg_l2(ie, t1aa, t1bb, t2bb, t2ab, t2ba, t2bb2, t2ab2, t2ba2, & 
                    iijkabb, iijkaab, iijkaba, iabijbb, iabijab, iabijba, &
                    tmp1, tmp2, tmp3, tmp4, tmp5, tmpe, &
                    iabcibb, iabciab, iabciba, & 
                    icore, e4t, e5st, ig)
use mod_iop
use mod_orbit
use mod_size
use mod_symm
use mod_t
implicit none 
integer prec2, isd2, igpu2, totgpu2
common/precsoc/prec2, isd2, igpu2, totgpu2
real*8, intent(in) :: e4t, e5st
real*8, intent(in) :: ie(*), t1aa(*), t1bb(*), t2bb(*), t2ab(*), t2ba(*)
real*8, intent(in) :: t2bb2(*), t2ab2(*), t2ba2(*) 
real*8, intent(in) :: iijkabb(*), iijkaab(*), iijkaba(*)
real*8, intent(in) :: iabijbb(*), iabijab(*), iabijba(*)
real*8, intent(in) :: iabcibb(*), iabciab(*), iabciba(*)
real*8, intent(in) :: tmp1(*), tmp2(*), tmp3(*), tmp4(*), tmp5(*), tmpe(*)
!
real*8, intent(in) :: icore(*)
real*8, intent(in),device :: ig(*)
!
integer iread, it, iter, itend, icopy, ileft,  ioffvvvoaa
integer ioffabi, ioffbbi
integer i0, i1, i2, i3, nsize
integer i4, i5, i6, i7, i8, i9, i10
integer i11, i12, i13, i14, i15, i16, i17, i18, i19, i20
integer i21, i22, i23, i24, i25, i26, i27, i28, i29, i30
integer i31, i32, i33, i34, nabcabi, nabcbbi
integer irrepi, numi, nabcbb, nabcab, nabcba, nabibb, nabiab, nabiba
integer nleft
integer ioffvvvobb(noa,8), ioffvvvoab(noa, 8), ioffvvvoba(noa, 8)
integer ioffvvoo2bb(noa, 8), ioffvvoo2ab(noa, 8), ioffvvoo2ba(noa, 8) 
integer ioffvoaa(noa, 8), ioffvobb(noa, 8)
integer ioffvooobb(noa,noa,8,8), ioffvoooab(noa, noa, 8, 8)
integer ioffvoooba(noa,noa, 8, 8), ioffvvoobb(noa,noa,8,8)
integer ioffvvooab(noa, noa, 8, 8), ioffvvooba(noa, noa, 8, 8)
!
integer irrepki, irrepij, irrepjk, irrepabc, numj, numk
integer irrepj, irrepk, i, j, k, iofftmp
integer, external :: idsymsoc, irpdso
real*8, external :: nrm2so
igpu=0; igpu2=0
i0 = 1
i1 = i0 + nvvoo
i2 = i1 + nvvoo
!
!calcioff
!for iabci--------------
call idxpqr_s(ioffvvvobb, noa, vrtb, vrtb, 0, vrtb, popb, 1, 1)
call idxpqr_s(ioffvvvoab, noa, vrta, vrtb, 1, vrta, popb, 1, 1)
call idxpqr_s(ioffvvvoba, noa, vrtb, vrta, 1, vrtb, popa, 1, 1)
!for t2aa2--------------
call idxpqr_s(ioffvvoo2bb, noa, vrtb, vrtb, 0, popb, popb, 1, 1)
call idxpqr_s(ioffvvoo2ab, noa, vrta, vrtb, 1, popa, popb, 1, 1)
call idxpqr_s(ioffvvoo2ba, noa, vrtb, vrta, 1, popb, popa, 1, 1)
!for t1
call idxpqr_s(ioffvoaa, noa, zorb, zorb, 1, vrta, popa, 1, 1)
call idxpqr_s(ioffvobb, noa, zorb, zorb, 1, vrtb, popb, 1, 1)
!for t2aa and iabij
call idxpq_rs(ioffvvoobb, noa, noa, vrtb, vrtb, 1, popb, popb, 1, 1)
call idxpq_rs(ioffvvooab, noa, noa, vrta, vrtb, 1, popa, popb, 1, 1)
call idxpq_rs(ioffvvooba, noa, noa, vrtb, vrta, 1, popb, popa, 1, 1)
!for iijka
call idxpq_rs(ioffvooobb, noa, noa, popb, vrtb, 1, popb, popb, 1, 1)
call idxpq_rs(ioffvoooab, noa, noa, popa, vrtb, 1, popa, popb, 1, 1)
call idxpq_rs(ioffvoooba, noa, noa, popb, vrta, 1, popb, popa, 1, 1)
!write(6,*)'ioffvooo', ioffvooo(2,3, 1,1)
!
!=======get t1
!
call getuhf(t1aa, 1, 'T1AA____')
call getuhf(t1bb, 1, 'T1BB____')
!
!=======get t2
!
call getuhf(icore(i0), 1, 'T2BB____')
call iexpso(icore(i0), icore(i1), vrtb, vrtb, 0, 0, popb, popb, 0, 1, 1)
call sstgenso(icore(i1), t2bb2, nsize, nvvbb, zorb,  popb, popb, & 
             icore, 1, '1324')
call iexpso(icore(i1), t2bb, vrtb, vrtb, 0, 1, popb, popb, 1, 0, 1)
!
call getuhf(t2ab, 1, 'T2AB____')
call sstgenso(t2ab, t2ab2, nsize, nvvab, zorb, popa, popb, & 
              icore, 1, '1324')
call isymtrso(t2ab, icore, vrta, vrtb, 1, 1, popa, popb, 1, 0, 1) 
call isymtrso(icore, t2ba, vrtb, vrta, 1, 0, popa, popb, 1, 1, 1) 
call sstgenso(t2ba, t2ba2, nsize, nvvab, zorb, popb, popa, & 
              icore, 1, '1324')

if(ldebug) then 
nsize = idsymsoc(1, vrtb, vrtb, 1, popb, popb, 1)
write(6,*)'norm of t2bb', nrm2so(nsize, t2bb, 1)
nsize = idsymsoc(1, vrtb, vrtb, 0, popb, popb, 1)
write(6,*)'norm of t2bb2', nrm2so(nsize, t2bb2, 1)
nsize = idsymsoc(1, vrta, vrtb, 1, popa, popb, 1)
write(6,*)'norm of t2ab', nrm2so(nsize, t2ab, 1)
write(6,*)'norm of t2ab2', nrm2so(nsize, t2ab2, 1)
write(6,*)'norm of t2ba', nrm2so(nsize, t2ba, 1)
write(6,*)'norm of t2ba2', nrm2so(nsize, t2ba2, 1)
endif
!
!=======get iabij(a<b,i<j) and transfer to iabij(a,b,i,j) 
!
call getuhf(icore(i0), 1, 'ABIJBB__')
call iexpso(icore(i0), icore(i1), vrtb, vrtb, 0, 0, popb, popb, 0, 1, 1)
call iexpso(icore(i1), iabijbb,   vrtb, vrtb, 0, 1, popb, popb, 1, 0, 1)
call getuhf(iabijab, 1, 'ABIJAB__')
call isymtrso(iabijab, icore, vrta, vrtb, 1, 1, popa, popb, 1, 0, 1) 
call isymtrso(icore, iabijba, vrtb, vrta, 1, 0, popa, popb, 1, 1, 1) 
!
!=======get iijka(i<j,k,a) and transfer to iijka(k,a,i,j)
!
call getuhf(icore(i0), 1, 'IJKABB__')
call itranspso(icore(i0), icore(i1), popb, popb, 0, popb, vrtb, 1, 1)
call iexpso(icore(i1), iijkabb, popb, vrtb, 1, 0, popb, popb, 0, 1, 1)
!
call getuhf(icore(i0), 1, 'IJKAAB__')
call itranspso(icore(i0), iijkaab, popa, popb, 1, popa, vrtb, 1, 1)
call getuhf(icore(i0), 1, 'IJKABA__')
call itranspso(icore(i0), iijkaba, popb, popa, 1, popb, vrta, 1, 1)
!
if(ldebug) then 
nsize = idsymsoc(1, popb, popb, 1, vrtb, popb, 1) 
write(6,*)'norm of iijkabb', nrm2so(nsize, iijkabb, 1)
endif
! 
!=======get iabci aa, ab and ba
!
!call getuhf(iabci, 1, 'ABCIAA__')
!nsize = idsymsoc(1, vrta, vrta, 0, vrta, popa, 1) 
!write(6,*)'norm of iabciaa', nrm2so(nsize, iabci, 1)
do irrepi = 1, nirrep
   numi = popb(irrepi)
   do i = 1, numi
   iofftmp = ioffvvvobb(i, irrepi)
   call getpqrso(iabcibb(iofftmp), i, irrepi, 1, 'ABCIBB__')
   iofftmp = ioffvvvoab(i, irrepi)
   call getpqrso(iabciab(iofftmp), i, irrepi, 1, 'ABCIAB__')
   enddo
enddo
do irrepi = 1, nirrep
   numi = popa(irrepi)
   do i = 1, numi
   iofftmp = ioffvvvoba(i, irrepi)
   call getpqrso(iabciba(iofftmp), i, irrepi, 1, 'ABCIBA__')
   enddo
enddo
if(ldebug) then 
nsize = idsymsoc(1, vrtb, vrtb, 0, vrtb, popb, 1) 
write(6,*)'norm of iabcibb', nrm2so(nsize, iabcibb, 1)
nsize = idsymsoc(1, vrta, vrtb, 1, vrta, popb, 1) 
write(6,*)'norm of iabciab', nrm2so(nsize, iabciab, 1)
nsize = idsymsoc(1, vrtb, vrta, 1, vrtb, popa, 1) 
write(6,*)'norm of iabciba', nrm2so(nsize, iabciba, 1)
endif
!
igpu=1; igpu2=1
do irrepk = 1, nirrep 
do irrepj = 1, nirrep
do irrepi = 1, nirrep 
   irrepij = dirprd(irrepi, irrepj) 
   irrepjk = dirprd(irrepj, irrepk) 
   irrepki = dirprd(irrepk, irrepi) 
   irrepabc = dirprd(irrepij, irrepk)
   nabcbb = max(idsymsoc(irrepi, vrtb, vrtb, 0, vrtb, zorb, 1), &
                idsymsoc(irrepj, vrtb, vrtb, 0, vrtb, zorb, 1))
   nabcab = max(idsymsoc(irrepi, vrta, vrtb, 1, vrta, zorb, 1), &
                idsymsoc(irrepj, vrta, vrtb, 1, vrta, zorb, 1))
   nabcba = idsymsoc(irrepk, vrtb, vrta, 1, vrtb, zorb, 1)
!
   nabcbbi = idsymsoc(irrepi, vrtb, vrtb, 0, vrtb, zorb, 1)
   nabcabi = idsymsoc(irrepi, vrta, vrtb, 1, vrta, zorb, 1)
!
   nabibb = max(idsymsoc(irrepi, vrtb, vrtb, 0, popb, zorb, 1), &
                idsymsoc(irrepj, vrtb, vrtb, 0, popb, zorb, 1))
   nabiab = max(idsymsoc(irrepi, vrta, vrtb, 1, popa, zorb, 1), &
                idsymsoc(irrepj, vrta, vrtb, 1, popa, zorb, 1))
   nabiba = idsymsoc(irrepk, vrtb, vrta, 1, popb, zorb, 1)
!
   numi = popb(irrepi)
   numj = popb(irrepj) 
   numk = popa(irrepk)
!
   i0  = 1
   i1  = i0  + nvb+nob          ! ieb
   i2  = i1  + nva+noa          ! iea
   i3  = i2  + nvomax           ! t1bb
   i4  = i3  + nvomax           ! t1aa
!
   i5  = i4  + nvvmax           ! t2bb_ij
   i6  = i5  + nvvmax           ! t2ba_ik
   i7  = i6  + nvvmax           ! t2ba_jk
   i8  = i7  + nvvmax           ! t2ab_ki
   i9  = i8  + nvvmax           ! t2ab_kj 
!
   i10 = i9  + nabibb           ! t2bb2_i
   i11 = i10 + nabibb           ! t2bb2_j
   i12 = i11 + nabiba           ! t2ba2_k
   i13 = i12 + nabiab           ! t2ab2_i
   i14 = i13 + nabiab           ! t2ab2_j
!
   i15 = i14 + nvomax           ! iijkabb_ij
   i16 = i15 + nvomax           ! iijkaba_ik
   i17 = i16 + nvomax           ! iijkaba_jk
   i18 = i17 + nvomax           ! iijkaab_ki
   i19 = i18 + nvomax           ! iijkaab_kj
!
   i20 = i19 + nvvmax           ! iabijbb_ij
   i21 = i20 + nvvmax           ! iabijba_jk
   i22 = i21 + nvvmax           ! iabijab_ik
!
   i23 = i22 + nvvv             ! tmp1
   i24 = i23 + nvvv             ! tmp2
   i25 = i24 + nvvv             ! tmp3
   i26 = i25 + nvvv             ! tmp4
   i27 = i26 + nvvv             ! tmp5 
   i28 = i27 + nvvv             ! tmpe
!
   i29 = i28 + nvvv             ! iabciab_j
   i30 = i29 + nvvv             ! iabcibb_j 
   i31 = i30 + nvvv             ! iabciba_k
   igpuleft = igpusize - i31
   if(igpuleft<nvvv*2) then 
      write(6,*)'error!!!! GPU memory not enough'
      write(6,*)'needed memory', (i31+nvvv*2)/1024/1024/4/isd,'mb'
      stop
   endif
   iread = min(igpuleft/nvvv/2, nvvvo/nvvv+1)   
   i32 = i31 + nvvv*iread       ! iabciab_i
   i33 = i32 + nvvv*iread       ! iabcibb_i
!
   call myicopy_g(ie(nva+noa+1), ig(i0), nvb+nob)
   call myicopy_g(ie,            ig(i1), nva+noa)
   call myicopy_g(t1bb, ig(i2), nvomax)
   call myicopy_g(t1aa, ig(i3), nvomax)
    if(irrepi>irrepj) cycle
    if(irrepi==irrepj) then
     iter = 0
     ileft = numi
     do it = 1, numi
        if(it==iter*iread+1) then
        itend = it+iread-1
        iter = iter + 1
        icopy = min(iread, ileft)
        if(icopy<0) icopy = icopy+iread
        if(ldebug) then
        write(6,'(A,I4, I4, I4)')'iter, iread and icopy',iter, iread, icopy
        endif
        call myicopy_g(iabcibb(ioffvvvobb(it,irrepi)),ig(i32), nabcbbi*icopy)
        call myicopy_g(iabciab(ioffvvvoab(it,irrepi)),ig(i31), nabcabi*icopy)
        ileft = numi-iter*iread
!
      do k = 1, numk
          call myicopy_g(t2ba2(ioffvvoo2ba(k,irrepk)), ig(i11), nabiba)
          call myicopy_g(iabciba(ioffvvvoba(k,irrepk)),ig(i30), nabcba) 
      do j = 2, numj
          call myicopy_g(t2ba(ioffvvooba(j,k,irrepj,irrepk)), ig(i6), nvvmax)
          call myicopy_g(t2ab(ioffvvooab(k,j,irrepk,irrepj)), ig(i8), nvvmax)
!
          call myicopy_g(t2bb2(ioffvvoo2bb(j,irrepj)), ig(i10), nabibb)
          call myicopy_g(t2ab2(ioffvvoo2ab(j,irrepj)), ig(i13), nabiab)
!         
          call myicopy_g(iijkaba(ioffvoooba(j,k,irrepj,irrepk)),ig(i16), nvomax)
          call myicopy_g(iijkaab(ioffvoooab(k,j,irrepk,irrepj)),ig(i18), nvomax)
!         
          call myicopy_g(iabijba(ioffvvooba(j,k,irrepj,irrepk)),ig(i20), nvvmax)
          call myicopy_g(iabcibb(ioffvvvobb(j,irrepj)),ig(i29), nabcbb)
          call myicopy_g(iabciab(ioffvvvoab(j,irrepj)),ig(i28), nabcab)
          ioffbbi = 0
          ioffabi = 0
      do i = 1, j-1
          if(i<it.or.i>itend) cycle
!
          call myicopy_g(t2bb(ioffvvoobb(i,j,irrepi,irrepj)), ig(i4), nvvmax)
          call myicopy_g(t2ba(ioffvvooba(i,k,irrepi,irrepk)), ig(i5), nvvmax)
          call myicopy_g(t2ab(ioffvvooab(k,i,irrepk,irrepi)), ig(i7), nvvmax)
!
          call myicopy_g(t2bb2(ioffvvoo2bb(i,irrepi)), ig(i9),  nabibb)
          call myicopy_g(t2ab2(ioffvvoo2ab(i,irrepi)), ig(i12), nabiab)
!
          call myicopy_g(iijkabb(ioffvooobb(i,j,irrepi,irrepj)),ig(i14), nvomax)
          call myicopy_g(iijkaba(ioffvoooba(i,k,irrepi,irrepk)),ig(i15), nvomax)
          call myicopy_g(iijkaab(ioffvoooab(k,i,irrepk,irrepi)),ig(i17), nvomax)
!
          call myicopy_g(iabijbb(ioffvvoobb(i,j,irrepi,irrepj)),ig(i19), nvvmax)
          call myicopy_g(iabijba(ioffvvooba(i,k,irrepi,irrepk)),ig(i21), nvvmax)
! 
!          call myicopy_g(iabcibb(ioffvvvobb(i,irrepi)),ig(i32), nabcbb)
!          call myicopy_g(iabciab(ioffvvvoab(i,irrepi)),ig(i31), nabcab)
! 
      call ccsdt_calcaab_2(ig(i0), ig(i1), ig(i2-1+ioffvobb(i,irrepi)), &
          ig(i2-1+ioffvobb(j,irrepj)), ig(i3-1+ioffvoaa(k,irrepk)), &
!
          ig(i4), ig(i5), ig(i6), ig(i7), ig(i8), &
!
          ig(i9), ig(i10), ig(i11), ig(i12), ig(i13), &
!
          ig(i14), ig(i15), ig(i16), ig(i17), ig(i18), &
!
          ig(i19), ig(i20), ig(i21), &
!
          ig(i22), ig(i23), ig(i24), ig(i25), ig(i26), ig(i27),  &
!
          ig(ioffbbi+i32), ig(i29), ig(i30), ig(ioffabi+i31), ig(i28), &
!
          irrepi, irrepj, irrepk, i, j, k,          &
          ioffvvvobb, ioffvvvoba, ioffvvvoab,    &
          ioffvvoo2bb, ioffvvoo2ba, ioffvvoo2ab,&
          ioffvobb, ioffvoaa,&
          ioffvooobb, ioffvoooba,&
          ioffvoooab, ioffvvoobb,&
          ioffvvooba, ioffvvooab,&
          icore, e4t, e5st,vrtb, vrta, popb, popa, 3) 
!
          ioffbbi = ioffbbi + nabcbbi
          ioffabi = ioffabi + nabcabi
      enddo
      enddo
      enddo
     endif
    enddo
    elseif(irrepi<irrepj) then
     iter = 0
     ileft = numi
     do it = 1, numi
        if(it==iter*iread+1) then
        itend = it+iread-1
        iter = iter + 1
        icopy = min(iread, ileft)
        if(icopy<0) icopy = icopy+iread
        if(ldebug) then
        write(6,'(A,I4, I4, I4)')'iter, iread and icopy',iter, iread, icopy
        endif
        call myicopy_g(iabcibb(ioffvvvobb(it,irrepi)),ig(i32), nabcbbi*icopy)
        call myicopy_g(iabciab(ioffvvvoab(it,irrepi)),ig(i31), nabcabi*icopy)
        ileft = numi-iter*iread

      do k = 1, numk
          call myicopy_g(t2ba2(ioffvvoo2ba(k,irrepk)), ig(i11), nabiba)
          call myicopy_g(iabciba(ioffvvvoba(k,irrepk)),ig(i30), nabcba) 
      do j = 1, numj 
          call myicopy_g(t2ba(ioffvvooba(j,k,irrepj,irrepk)), ig(i6), nvvmax)
          call myicopy_g(t2ab(ioffvvooab(k,j,irrepk,irrepj)), ig(i8), nvvmax)
!
          call myicopy_g(t2bb2(ioffvvoo2bb(j,irrepj)), ig(i10), nabibb)
          call myicopy_g(t2ab2(ioffvvoo2ab(j,irrepj)), ig(i13), nabiab)
!         
          call myicopy_g(iijkaba(ioffvoooba(j,k,irrepj,irrepk)),ig(i16), nvomax)
          call myicopy_g(iijkaab(ioffvoooab(k,j,irrepk,irrepj)),ig(i18), nvomax)
!         
          call myicopy_g(iabijba(ioffvvooba(j,k,irrepj,irrepk)),ig(i20), nvvmax)
          call myicopy_g(iabcibb(ioffvvvobb(j,irrepj)),ig(i29), nabcbb)
          call myicopy_g(iabciab(ioffvvvoab(j,irrepj)),ig(i28), nabcab)
          ioffbbi = 0
          ioffabi = 0
      do i = 1, numi
          if(i<it.or.i>itend) cycle
          call myicopy_g(t2bb(ioffvvoobb(i,j,irrepi,irrepj)), ig(i4), nvvmax)
          call myicopy_g(t2ba(ioffvvooba(i,k,irrepi,irrepk)), ig(i5), nvvmax)
          call myicopy_g(t2ab(ioffvvooab(k,i,irrepk,irrepi)), ig(i7), nvvmax)
!
          call myicopy_g(t2bb2(ioffvvoo2bb(i,irrepi)), ig(i9),  nabibb)
          call myicopy_g(t2ab2(ioffvvoo2ab(i,irrepi)), ig(i12), nabiab)
!
          call myicopy_g(iijkabb(ioffvooobb(i,j,irrepi,irrepj)),ig(i14), nvomax)
          call myicopy_g(iijkaba(ioffvoooba(i,k,irrepi,irrepk)),ig(i15), nvomax)
          call myicopy_g(iijkaab(ioffvoooab(k,i,irrepk,irrepi)),ig(i17), nvomax)
!
          call myicopy_g(iabijbb(ioffvvoobb(i,j,irrepi,irrepj)),ig(i19), nvvmax)
          call myicopy_g(iabijba(ioffvvooba(i,k,irrepi,irrepk)),ig(i21), nvvmax)
! 
!          call myicopy_g(iabcibb(ioffvvvobb(i,irrepi)),ig(i32), nabcbb)
!          call myicopy_g(iabciab(ioffvvvoab(i,irrepi)),ig(i31), nabcab)
! 
      call ccsdt_calcaab_2(ig(i0), ig(i1), ig(i2-1+ioffvobb(i,irrepi)), &
          ig(i2-1+ioffvobb(j,irrepj)), ig(i3-1+ioffvoaa(k,irrepk)), &
!
          ig(i4), ig(i5), ig(i6), ig(i7), ig(i8), &
!
          ig(i9), ig(i10), ig(i11), ig(i12), ig(i13), &
!
          ig(i14), ig(i15), ig(i16), ig(i17), ig(i18), &
!
          ig(i19), ig(i20), ig(i21), &
!
          ig(i22), ig(i23), ig(i24), ig(i25), ig(i26), ig(i27), &
!
          ig(ioffbbi+i32), ig(i29), ig(i30), ig(ioffabi+i31), ig(i28), &
!
          irrepi, irrepj, irrepk, i, j, k,          &
          ioffvvvobb, ioffvvvoba, ioffvvvoab,    &
          ioffvvoo2bb, ioffvvoo2ba, ioffvvoo2ab,&
          ioffvobb, ioffvoaa,&
          ioffvooobb, ioffvoooba,&
          ioffvoooab, ioffvvoobb,&
          ioffvvooba, ioffvvooab,&
          icore, e4t, e5st,vrtb, vrta, popb, popa, 3)
!
          ioffbbi = ioffbbi + nabcbbi
          ioffabi = ioffabi + nabcabi
      enddo
      enddo
      enddo
      endif
     enddo
    endif
enddo
enddo
enddo
igpu=0; igpu2=0
write(6,*)'e4t and e5st for bba', e4t, e5st 
return
end
